# delimit ;  
set more 1 ;  
local UNIX 0;
drop _all;
set mem 600m;


*global folder "C:\research";
*global folder "C:\Users\Jeremy\Dropbox";
*global folder "\\ad.ucl.ac.uk\homer\zctpmer\downloads";
*global folder "C:\dropbox";
global folder "D:\Dropbox";
*global folder "\\econ-san-fs\Home4\uctpkp0\SystemFolder\Windows7\Desktop";




         local saved  "$folder\hrs\merge\";
         local trackd "$folder\hrs\tracker\";
         local wave1d "$folder\hrs\wave1\";
         local wave2d "$folder\hrs\wave2\";
         local wave3d "$folder\hrs\wave3\";
         local wave4d "$folder\hrs\wave4\";
         local wave5d "$folder\hrs\wave5\";
		 local wave6d "$folder\hrs\wave6\";
         local wave7d "$folder\hrs\wave7\";
         local wave8d "$folder\hrs\wave8\";
		 local wave9d "$folder\hrs\wave9\";
		 local wave10d "$folder\hrs\wave10\";
		 local wave11d "$folder\hrs\wave11\";
		 local wave12d "$folder\hrs\wave12\";
		 local wave13d "$folder\hrs\wave13\";
		 local wave14d "$folder\hrs\wave14\";
	     local wave15d "$folder\hrs\wave15\";
         local aheadw1 "$folder\hrs\aheadw1\";
         local aheadw2 "$folder\hrs\aheadw2\";

capture log close;
log using `saved'merge1.log, replace ; 

/*
use `wave5d'H00CS_R;
gen temp=real(HHID);
drop HHID;
gen HHID=temp;
drop temp;
gen temp=real(PN);
drop PN;
gen PN=temp;
drop temp;
keep HHID PN G480 G482; 
sort HHID PN;
save `saved'H00CS_R_to_merge, replace;
drop _all;

use `wave4d'H98CS_R;
gen temp=real(HHID);
drop HHID;
gen HHID=temp;
drop temp;
gen temp=real(PN);
drop PN;
gen PN=temp;
drop temp;
keep HHID PN F461 F459;
sort HHID PN;
save `saved'H98CS_R_to_merge, replace;
drop _all;

use `saved'H00CS_R_to_merge;
sort HHID PN;
merge HHID PN using `saved'H98CS_R_to_merge;
drop _merge;
sort HHID PN;
merge HHID PN 
*/

use `saved'employ;
*drop _merge;
*lastly , the pre-generated files of insure and asset;
*use `saved'employ;
sort HHID PN;
merge HHID PN using `saved'demog;
drop _merge;
sort HHID PN;
merge HHID PN using `saved'asset;
drop _merge;
sort HHID PN;
merge HHID PN using `saved'income;
drop _merge;
sort HHID PN;
merge HHID PN using `saved'insure;
drop _merge;
sort HHID PN;
merge HHID PN using `saved'healstat;
drop _merge;
sort HHID PN;
merge HHID PN using `saved'mortality;
drop _merge;
sort HHID PN;
merge HHID PN using `saved'medcost;
drop _merge;
compress;
sort HHID PN;
merge HHID PN using `saved'exitmedcost;
drop _merge;
sort HHID PN;
merge HHID PN using `saved'exitassets;
drop _merge;
compress;
sort HHID PN;
merge HHID PN using `saved'exithealutil;
drop _merge;
compress;
sort HHID PN;
merge HHID PN using `saved'settle;
drop _merge;
sort HHID PN;
merge HHID PN using `saved'healutil;
drop _merge;
sort HHID PN;
merge HHID PN using `saved'pension;
drop _merge;
sort HHID PN;
merge HHID PN using `saved'tenure;
drop _merge;
sort HHID PN;
merge HHID PN using `saved'retplans;
drop _merge;
sort HHID PN; 


* drop two observations with the same HHID and PN -- not sure what went wrong;
/* redundant now */
/*
gen long h=HHID;
replace h=h*1000+PN;
egen dude=count(h), by (h);
sort h;
drop if dude>1 & h==h[_n-1];
drop h dude;
*/

*drop V5* V3* V9*;
sleep 1000;
save `saved'merge1, replace;
drop _all;
log close ;









